﻿InterrupterTargets = {
	"target",
	"focus",
	"mouseover",
	"boss1",
	"boss2",
	"boss3",
	"boss4"
}

function GetInterruptSpellID()
	
	local _, playerClass = UnitClass("player")
	
	if playerClass == "DEATHKNIGHT" then
		return 47528
	elseif playerClass == "DRUID" then
		local catForm = UnitBuffID("player", 768)
		if catForm ~= nil then
			return 80965
		else
			return 80964
		end
	elseif playerClass == "HUNTER" then
		return 147362
	elseif playerClass == "MAGE" then
		return 2139
	elseif playerClass == "MONK" then
		return 116705			
	elseif playerClass == "PALADIN" then
		return 96231
	elseif playerClass == "PRIEST" then
		return 15487
	elseif playerClass == "ROGUE" then
		return 1766
	elseif playerClass == "SHAMAN" then
		return 57994
	elseif playerClass == "WARLOCK" then
		return 19647
	elseif playerClass == "WARRIOR" then
		return 6552
	else
		return 0
	end
	
end

function Interrupter(customTarget)

	if not UnitExists(customTarget) then return false end
	if not UnitCanAttack("player", customTarget) then return false end
	
	local interruptSpell = GetInterruptSpellID()

	if interruptSpell ~= 0 then
		local interruptName = GetSpellInfo(interruptSpell)
		if not IsSpellAvailable(interruptSpell) then
			return false
		end
		if IsSpellInRange(interruptName, customTarget) ~= 1 then
			return false
		end
	else
		return false
	end
	
	local castName, _, _, _, castStartTime, castEndTime, _, _, castInterruptable = UnitCastingInfo(customTarget)
	local channelName, _, _, _, channelStartTime, channelEndTime, _, channelInterruptable = UnitChannelInfo(customTarget)
	
	if channelName ~= nil then
		--target is channeling a spell that is interruptable
		--load the channel variables into the cast variables to make logic a little easier.
		castName = channelName
		castStartTime = channelStartTime
		castEndTime = channelEndTime
		castInterruptable = channelInterruptable
	end
	
	--This is actually "Not Interruptable"... so lets swap it around to use in the positive.
	if castInterruptable == false then castInterruptable = true else castInterruptable = false end
	
	if castInterruptable then
		
		--target is casting something that is interruptable.
		--the following 2 variables are named logically... value is in seconds.
		local timeSinceStart = (GetTime() * 1000 - castStartTime) / 1000
		local timeLeft = ((GetTime() * 1000 - castEndTime) * -1) / 1000
		local castTime = castEndTime - castStartTime
		local currentPercent = timeSinceStart / castTime * 100000
		
		--This is the delay before interrupting in seconds.
		local InterruptDelay = 0.05
		
		--This is the percentage of the cast to wait before interrupting
		if UnitIsPlayer(customTarget) then
			InterruptPercent = math.random(60,80)
		else
			InterruptPercent = math.random(70,75)
		end
		
		--interrupt percentage check
		if currentPercent < InterruptPercent then return false end
		
		--minimum interrupt time.
		if timeSinceStart - InterruptDelay < 0 then return false end
		
		if IsCasting() or IsChanneling() then SpellStopCasting() end
		
		CastSpellByName(GetSpellInfo(interruptSpell),customTarget)
		return true
		
	end
	
end